home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C18 / Format.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.1 KB  |  75 lines

  1. //: C18:Format.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Formatting functions
  7. #include <fstream>
  8. using namespace std;
  9. #define D(A) T << #A << endl; A
  10. ofstream T("format.out");
  11.  
  12. int main() {
  13.   D(int i = 47;)
  14.   D(float f = 2300114.414159;)
  15.   char* s = "Is there any more?";
  16.  
  17.   D(T.setf(ios::unitbuf);)
  18. //  D(T.setf(ios::stdio);)  // SOMETHING MAY HAVE CHANGED
  19.  
  20.   D(T.setf(ios::showbase);)
  21.   D(T.setf(ios::uppercase);)
  22.   D(T.setf(ios::showpos);)
  23.   D(T << i << endl;) // Default to dec
  24.   D(T.setf(ios::hex, ios::basefield);)
  25.   D(T << i << endl;)
  26.   D(T.unsetf(ios::uppercase);)
  27.   D(T.setf(ios::oct, ios::basefield);)
  28.   D(T << i << endl;)
  29.   D(T.unsetf(ios::showbase);)
  30.   D(T.setf(ios::dec, ios::basefield);)
  31.   D(T.setf(ios::left, ios::adjustfield);)
  32.   D(T.fill('0');)
  33.   D(T << "fill char: " << T.fill() << endl;)
  34.   D(T.width(10);)
  35.   T << i << endl;
  36.   D(T.setf(ios::right, ios::adjustfield);)
  37.   D(T.width(10);)
  38.   T << i << endl;
  39.   D(T.setf(ios::internal, ios::adjustfield);)
  40.   D(T.width(10);)
  41.   T << i << endl;
  42.   D(T << i << endl;) // Without width(10)
  43.  
  44.   D(T.unsetf(ios::showpos);)
  45.   D(T.setf(ios::showpoint);)
  46.   D(T << "prec = " << T.precision() << endl;)
  47.   D(T.setf(ios::scientific, ios::floatfield);)
  48.   D(T << endl << f << endl;)
  49.   D(T.setf(ios::fixed, ios::floatfield);)
  50.   D(T << f << endl;)
  51.   D(T.setf(0, ios::floatfield);) // Automatic
  52.   D(T << f << endl;)
  53.   D(T.precision(20);)
  54.   D(T << "prec = " << T.precision() << endl;)
  55.   D(T << endl << f << endl;)
  56.   D(T.setf(ios::scientific, ios::floatfield);)
  57.   D(T << endl << f << endl;)
  58.   D(T.setf(ios::fixed, ios::floatfield);)
  59.   D(T << f << endl;)
  60.   D(T.setf(0, ios::floatfield);) // Automatic
  61.   D(T << f << endl;)
  62.  
  63.   D(T.width(10);)
  64.   T << s << endl;
  65.   D(T.width(40);)
  66.   T << s << endl;
  67.   D(T.setf(ios::left, ios::adjustfield);)
  68.   D(T.width(40);)
  69.   T << s << endl;
  70.  
  71.   D(T.unsetf(ios::showpoint);)
  72.   D(T.unsetf(ios::unitbuf);)
  73. //  D(T.unsetf(ios::stdio);) // SOMETHING MAY HAVE CHANGED
  74. } ///:~
  75.